package com.diapoclic.servlet;


import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.diapoclic.entitybean.User;
import com.diapoclic.entitydao.FolderDao;
import com.diapoclic.entitymanager.DocumentManager;
import com.diapoclic.entitymanager.FolderManager;
import com.diapoclic.entitymanager.UserManager;

public class AddPhotoToFolderServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)						
			throws IOException, ServletException{
		
		////////////////////////////////UTILISATION DES COOKIES///////////////////////////
				
		User sessionUser=new User();
		UserManager userMana = new UserManager();
		
		sessionUser=userMana.getSessionUser(request.getCookies());

		
		
		/// Recupération de la list de document et de la liste d'albums
		DocumentManager docMana=new DocumentManager();
		FolderManager folderMana=new FolderManager();
		
		try {
		request.setAttribute("folderlist",folderMana.getFolderDao().findByIdUser(sessionUser.getIdUser()));
		} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		
		
		try {
		request.setAttribute("documentlist",docMana.getDocumentDao().findByIdUser(sessionUser.getIdUser()));
		} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		
		
		request.getRequestDispatcher("addPhotoToFolder.jsp").forward(request, response);
		

	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)						
											throws IOException, ServletException{
		
		////////////////////////////////UTILISATION DES COOKIES///////////////////////////
		
		User sessionUser=new User();
		UserManager userMana = new UserManager();
		
		sessionUser=userMana.getSessionUser(request.getCookies());

		
		//récupération des choix 
    	
		int idFolderMaj=0;
		int erreur=0;
		System.out.println(request.getParameter("folder"));
		if(request.getParameter("folder")!=null)
			idFolderMaj=Integer.parseInt(request.getParameter("folder"));
		else     		erreur=1;

		String[] cboxphoto = request.getParameterValues("photos");
        FolderManager folderMana=new FolderManager();
        if (cboxphoto==null || erreur==1) 
        	doGet(request,response);


        
        
        //Ajout des photo dans l'album choisi
        for (int i = 0; i < cboxphoto.length; ++i)
        { 
	        try {
				folderMana.getFolderDao().addDocumentInFolder(Integer.parseInt(cboxphoto[i]), idFolderMaj);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	
		
		request.getRequestDispatcher("album.jsp").forward(request, response);

	}	
}

